﻿namespace Smart.Utils.UI.BarcodeLib
{
    using System;
    using System.Collections;

    internal class Codabar : BarcodeCommon, IBarcode
    {
        private Hashtable Codabar_Code = new Hashtable();

        public Codabar(string input)
        {
            base.Raw_Data = input;
        }

        private string Encode_Codabar()
        {
            if (base.Raw_Data.Length < 2)
            {
                throw new Exception("ECODABAR-1: Data format invalid. (Invalid length)");
            }
            char ch2 = base.Raw_Data[0];
            string str3 = ch2.ToString().ToUpper().Trim();
            if ((str3 == null) || (((str3 != "A") && (str3 != "B")) && ((str3 != "C") && (str3 != "D"))))
            {
                throw new Exception("ECODABAR-2: Data format invalid. (Invalid START character)");
            }
            ch2 = base.Raw_Data[base.Raw_Data.Trim().Length - 1];
            str3 = ch2.ToString().ToUpper().Trim();
            if ((str3 == null) || (((str3 != "A") && (str3 != "B")) && ((str3 != "C") && (str3 != "D"))))
            {
                throw new Exception("ECODABAR-3: Data format invalid. (Invalid STOP character)");
            }
            string str = "";
            this.init_Codabar();
            foreach (char ch in base.Raw_Data)
            {
                str = str + this.Codabar_Code[ch].ToString() + "0";
            }
            str = str.Remove(str.Length - 1);
            this.Codabar_Code.Clear();
            return str;
        }

        private void init_Codabar()
        {
            this.Codabar_Code.Clear();
            this.Codabar_Code.Add('0', "101010011");
            this.Codabar_Code.Add('1', "101011001");
            this.Codabar_Code.Add('2', "101001011");
            this.Codabar_Code.Add('3', "110010101");
            this.Codabar_Code.Add('4', "101101001");
            this.Codabar_Code.Add('5', "110101001");
            this.Codabar_Code.Add('6', "100101011");
            this.Codabar_Code.Add('7', "100101101");
            this.Codabar_Code.Add('8', "100110101");
            this.Codabar_Code.Add('9', "110100101");
            this.Codabar_Code.Add('-', "101001101");
            this.Codabar_Code.Add('$', "101100101");
            this.Codabar_Code.Add(':', "1101011011");
            this.Codabar_Code.Add('/', "1101101011");
            this.Codabar_Code.Add('.', "1101101101");
            this.Codabar_Code.Add('+', "101100110011");
            this.Codabar_Code.Add('A', "1011001001");
            this.Codabar_Code.Add('B', "1010010011");
            this.Codabar_Code.Add('C', "1001001011");
            this.Codabar_Code.Add('D', "1010011001");
            this.Codabar_Code.Add('a', "1011001001");
            this.Codabar_Code.Add('b', "1010010011");
            this.Codabar_Code.Add('c', "1001001011");
            this.Codabar_Code.Add('d', "1010011001");
        }

        public string Encoded_Value
        {
            get
            {
                return this.Encode_Codabar();
            }
        }
    }
}

